package com.shenfa.action;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.shenfa.action.upload.DownloadAction;
import com.shenfa.bean.City;
import com.shenfa.service.CityService;

import com.shenfa.util.ResultObject;

/**
 * 城市管理ACTION
 * 
 * @author yanglong
 *
 */
public class CityAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	
	@Autowired
	private CityService cityService;
	
	private City city;
	private static Logger log=Logger.getLogger(DownloadAction.class);
	private int start;
	private int limit;
	
	/**
	 * 根据对象条件查询集合
	 */
	@SuppressWarnings("unchecked")
	public String list(){
		log.debug("city="+city+",start="+start+",limit="+limit);
		List<City> list = cityService.queryList(city, start, limit);
		if(list != null)
		result = ResultObject.build("0", list);
		result.put("total", cityService.queryCount(city));
		return SUCCESS;
	}

	/**
	 * 添加城市
	 */
	public String add() {
		log.debug("start="+start+",limit="+limit+",cityName="+city.getCityname()+",cityNo="+city.getCityno()+",flag="+city.getFlag()+",parentNo="+city.getParentno());
		int type = cityService.insertCity(city);
		if(type == 0){
			result = ResultObject.build(String.valueOf(type), "添加成功！");
		} else {
			result = ResultObject.build(String.valueOf(type), "操作失败！");
		}
		return SUCCESS;
	}

	/**
	 * 删除城市
	 */
	public String del() {
		log.debug("cityNo="+city.getCityno());
		int type = cityService.deleteCity(city);
		if(type == 0){
			result = ResultObject.build(String.valueOf(type), "删除成功！");
		} else {
			result = ResultObject.build(String.valueOf(type), "操作失败！");
		}
		return SUCCESS;
	}

	/**
	 * 查询总记录条数
	 * @param city	查询条件
	 * @return	总记录条数
	 */
	public int getCount(City city){
		log.debug("cityNo="+city.getCityno()+",parentNo="+city.getParentno());
		return cityService.queryCount(city);
	}

	public String upd() {
		log.debug("start="+start+",limit="+limit+",cityName="+city.getCityname()+",cityNo="+city.getCityno()+",flag="+city.getFlag()+",parentNo="+city.getParentno());
		int type = cityService.updateCity(city);
		if(type == 0){
			result = ResultObject.build(String.valueOf(type), "修改成功！");
		} else {
			result = ResultObject.build(String.valueOf(type), "操作失败！");
		}
		return SUCCESS;
	}
	
	public City getCity() {
		return city;
	}

	public void setCity(City city) {
		this.city = city;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}
	
}
